// print() 不会自动回到左上角，连续打印会从上次光标位置继续，要用 setCursor()
// 如果要刷新文字，每次最好 clear() → setCursor() → print() → show()
#include <FastLED.h>
#include <FastLED_NeoMatrix.h>
#include <WiFi.h>
#include <Button2.h>
#include <time.h>
#include "MyFont.h"

// 引脚定义
#define LED_PIN 5
#define BUTTON_PIN 48

// 矩阵定义
#define kMatrixWidth 32
#define kMatrixHeight 8
#define BRIGHTNESS 255
#define COLOR_ORDER GRB
#define CHIPSET WS2812
#define NUM_LEDS ((kMatrixWidth) * (kMatrixHeight))

// WIFI配置
#define WIFI_RETRY_COUNT 10

// 初始化变量
CRGB leds[NUM_LEDS];
FastLED_NeoMatrix *matrix;
uint16_t mainColor;
uint16_t weekColor;
int clockColor[3];

void WifiSearchAnim() {
  matrix->clear();
  matrix->setCursor(0, 0);
  matrix->setTextColor(matrix->Color(0, 255, 0));  // 绿色
  matrix->print("COON");
  matrix->show();
  delay(1000);
}


void setup() {
  Serial.begin(115200);
  FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  matrix = new FastLED_NeoMatrix(leds, 8, 8, 4, 1, NEO_MATRIX_TOP + NEO_MATRIX_LEFT + NEO_MATRIX_ROWS + NEO_MATRIX_PROGRESSIVE);
  matrix->setFont(&MyFont);
  matrix->setTextWrap(false);
  // matrix->setTextColor(matrix->Color(0, 255, 0));
  matrix->setBrightness(5);
  // matrix->clear();
  // matrix->show();

  int r = 0;
  int g = 250;
  int b = 250;
  clockColor[0] = r;
  clockColor[1] = g;
  clockColor[2] = b;
  mainColor = matrix->Color(clockColor[0], clockColor[1], clockColor[2]);
  // 时间下面的星期条颜色，最大的RGB值不变，其他两个取反色
  int maxRGB = max(r, max(g, b));  // 求出RGB三色里的最大值
  bool findMax = false;            // 是否找到最大值，防止3个值一样大，都不取反
  int weekR, weekG, weekB;
  if (r == maxRGB) {
    findMax = true;
    weekR = r;
  } else {
    weekR = 255 - r;
  }
  if (g == maxRGB && !findMax) {
    findMax = true;
    weekG = g;
  } else {
    weekG = 255 - g;
  }
  if (b == maxRGB && !findMax) {
    findMax = true;
    weekB = b;
  } else {
    weekB = 255 - b;
  }
  weekColor = matrix->Color(weekR, weekG, weekB);

  int count = 0;
  WiFi.begin("Redmi_01C6", "88888888");
  delay(500);
  while (WiFi.status() != WL_CONNECTED) {
    count++;
    if (count > WIFI_RETRY_COUNT) {
      break;
    }
    WifiSearchAnim();
  }
  matrix->clear();
  matrix->setCursor(0, 0);
  matrix->print("OK");
  matrix->show();
  delay(5000);

  // 获取 NTP 时间
  configTime(8 * 3600, 0, "pool.ntp.org");  // 中国时区 UTC+8
}

void loop() {
  struct tm timeinfo;
  if (getLocalTime(&timeinfo)) {
    char buf[16];
    snprintf(buf, sizeof(buf), "%0.2d:%0.2d:%0.2d", timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec);

    // 清屏
    matrix->fillScreen(0);
    // 设置位置
    matrix->setCursor(2, 5);
    matrix->setTextColor(mainColor);  // 青色
    matrix->print(buf);

    // 绘制星期
    int weekNum = timeinfo.tm_wday;
    if (weekNum == 0) {
      weekNum = 7;
    }
    // 绘制6条直线(除了当天的)
    for (int i = 1; i <= 7; i++) {
      if (i == weekNum) {
        continue;
      }
      matrix->drawFastHLine(2 + (i - 1) * 4, 7, 3, weekColor);
    }
    matrix->drawFastHLine(2 + (weekNum - 1) * 4, 7, 3, mainColor);
    matrix->show();
  }
  delay(1000);
}
